//
//  AutomaticImage.m
//  Newproject3
//
//  Created by rimi on 16/9/23.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "AutomaticImage.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface AutomaticImage ()<UIScrollViewDelegate>{
    NSTimer * _timer;
    CGFloat _wd;
    CGFloat _ht;
    NSInteger _time;//图片循环时间
}

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSMutableArray *imageArray;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic, assign) NSInteger currentImageIndex;

@end

@implementation AutomaticImage

-(instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource duringTime:(CGFloat)time{
    self = [super initWithFrame:frame];
    if (self) {
        
        _wd = frame.size.width;
        _ht = frame.size.height;
        self.currentImageIndex = 0;
        _time = time;
        self.dataArray = dataSource;
        
        [self createTimer];
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
        
}
    return self;
}

#pragma mark -- nstimer
// 创建timer
-(void)createTimer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(action_Timer) userInfo:nil repeats:YES];
    }
}

-(void)action_Timer{
    [self.scrollView setContentOffset:CGPointMake(2 * _wd, 0) animated:YES];
}

// 开启timer
-(void)startTimer{
    _timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
//    if (_timer) {
//        [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
//    }
}
//暂停timer
-(void)pauseTimer{
    _timer.fireDate = [NSDate distantFuture];
//    if (_timer) {
//        [_timer setFireDate:[NSDate distantFuture]];
//    }
}

#pragma mark -- 滑动

-(void)leftScroll{
    self.currentImageIndex ++;
    if (self.currentImageIndex >= self.dataArray.count) {
        self.currentImageIndex = 0;
    }
    [self dynamicImage];
}

-(void)rightScroll{
    self.currentImageIndex --;
    if (self.currentImageIndex < 0 ) {
        self.currentImageIndex = self.dataArray.count - 1;
    }
    [self dynamicImage];
}

-(void)dynamicImage{
    NSInteger page = self.currentImageIndex;
    if (page >= self.dataArray.count) {
        page = 0;
    }
    //下标和图片对应
    self.pageControl.currentPage = page;
    
    for (NSInteger i = 0 ; i < 3 ; i ++) {
        if (page >= self.dataArray.count) {
            page = 0;
        }
        [self.imageArray[i] sd_setImageWithURL:[NSURL URLWithString:self.dataArray[page++]]];
    }
    [self.scrollView setContentOffset:CGPointMake(_wd, 0)];
}

#pragma mark -- UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x <= 0) {
        [self rightScroll];
    }
    if (scrollView.contentOffset.x >= 2 * _wd) {
        [self leftScroll];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self pauseTimer];
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}
#pragma mark -- getter

-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _wd, _ht)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        for (int i = 0 ; i < 3; i ++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*_wd, 0, _wd, _ht)];
            [imageView sd_setImageWithURL:self.dataArray[i]];
            [self.scrollView addSubview:imageView];
            imageView.userInteractionEnabled = YES;
            [self.imageArray addObject:imageView];
        }
        // 偏移总大小
        _scrollView.contentSize = CGSizeMake(_wd *3, _ht);
        [_scrollView setContentOffset:CGPointMake(_wd,0)];

    }
    return _scrollView;
}

-(UIPageControl *)pageControl{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_wd *0.7, _ht*0.85, _wd*0.3, _ht*0.15)];
        _pageControl.numberOfPages = self.dataArray.count;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

-(NSMutableArray *)imageArray{
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}
@end
